<?php

class activity extends head {

 function activity() {
  $this->table = "dump.activity";
  $this->getmetadata();
  $this->maintainhist = true;
  $this->createblank();
  $this->header[2] = "0";
  $_SESSION[$this->table] = $this;
 }

 function readactivity($userid) {
  $this->readdb($userid);
 }

 function writeactivity($userid) {
  if ($this->applyfromrequest()) {
   $this->writedb();
  }
 }

 function writefromrequest($activity,$activitydepends) {
  $sql = '' ;
  $jobid = '';
  foreach (json_decode($activity) as $id=>$activityobj) {
   $this->header = $activityobj;
   $this->header[2] = '0';
   $this->header[9] = '0';
//   $this->header[10] = '2014-01-01';
   $sql .= $this->makeinsertsql().";\n";
   $jobid = $activityobj[8];
  }
  sqlexec("delete from dump.activitydepends where itemid in (select itemid from dump.activity where jobid = '$jobid')");
  sqlexec("delete from dump.activity where jobid = '$jobid'");
 
  foreach(json_decode($activitydepends) as $id=>$item) {
   $sql .= "insert into dump.activitydepends values('$item[0]','$item[1]') ; \n"; 
  }
  sqlmultiquery($sql);
 
 }

 function readjob($jobid) {
  $ans = sqlreadkeyarray("select * from dump.activity ");
  $ans = json_encode($ans);
  $dep = sqlreadarray("select * from dump.activitydepends ");
  $dep = json_encode($dep);
  return array($ans,$dep) ;
 }

 function writedb() {
  // Overridden since this is an upsert
  $success = true;
  $dblink = getconn();
  foreach ($this->header as $id => $item) {
   $this->header[$id] = mysqli_real_escape_string($dblink, $item);
  }
  $this->markedited();
  $stmt = $this->makeupsertsql();
  $this->result = mysqli_query($dblink, $stmt);

  if (!$this->result) {
   $this->result = "E04: " . mysqli_error($dblink) . "\n" . $stmt;
   nonterminal_error($this->result);
   $this->header[2] -= 1;
   $success = false;
  } else {
   if ($dblink->insert_id) {
    $this->header[0] = $dblink->insert_id;
   }

   $_SESSION["lastid"] = $this->header[0];
   unset($_SESSION[get_class($this)]);
   unset($_SESSION[$this->table]);
  }
  mysqli_close($dblink);
  return $success;
 }


 function applyfromrequest() {
  $this->valid = true;
  // Goes through request. Applies if legal, otherwise error.
  $this->valids = array_fill(0,$this->atts,'');


  $this->header[0] = authenticate();
  if($f = @$_REQUEST['departlat']) {
   if(preg_match("/^-{0,1}[0-9]{0,3}[.]{0,1}[0-9]{0,15}$/",$f)) {
    $this->header[5] = $f;
   } else {
    $this->valids[5] = "Must be a latitude";
   }
  }

  if($f = @$_REQUEST['departlng']) {
   if(preg_match("/^-{0,1}[0-9]{0,3}[.]{0,1}[0-9]{0,15}$/",$f)) {
    $this->header[6] = $f;
   } else {
    $this->valids[6] = "Must be a longitude";
   }
  }

  if($f = @$_REQUEST['destlat']) {
   if(preg_match("/^-{0,1}[0-9]{0,3}[.]{0,1}[0-9]{0,15}$/",$f)) {
    $this->header[7] = $f;
   } else {
    $this->valids[7] = "Must be a latitude";
   }
  }

  if($f = @$_REQUEST['destlng']) {
   if(preg_match("/^-{0,1}[0-9]{0,3}[.]{0,1}[0-9]{0,15}$/",$f)) {
    $this->header[8] = $f;
   } else {
    $this->valids[8] = "Must be a longitude";
   }
  }

  if($f = @$_REQUEST['destaddress']) {
   if(strlen($f) > 10) {
    $this->header[9] = $f;
   } else {
    $this->valids[9] = "Must be a longitude";
   }
  }

  if($f = @$_REQUEST['departaddress']) {
   if(strlen($f) > 10) {
    $this->header[1] = $f;
   } else {
    $this->valids[1] = "Must be an address";
   }
  }

  $this->valid = every($this->valids,"");
  if($this->valid) {
   if($this->header[0] == "")
    $this->valids[0] = "Incomplete";
   if($this->header[1] == "")
    $this->valids[1] = "Incomplete";
   $this->valid = every($this->valids,"");
  }
  return $this->valid;

 }

}

?>